# 16.1.10 错误检查
import csv
from datetime import datetime
import matplotlib.pyplot as plt
import seaborn as sns

filename = 'data/death_valley_2018_simple.csv'

with open(filename) as f:
    reader = csv.reader(f)
    header = next(reader)

    dates, highs, lows = [], [], []
    for col in reader:
        cur_date = datetime.strptime(col[2], '%Y-%m-%d')
        try:
            high = int(col[4])
            low = int(col[5])
        except ValueError:
            print(f'Missing data from {cur_date}')
        else:
            dates.append(cur_date)
            highs.append(high)
            lows.append(low)

# plt.style.use('seaborn')
sns.set(style='darkgrid', font_scale=2)
fig, ax = plt.subplots()

# 16.1.9 给图表区域着色
ax.plot(dates, highs, c='red', alpha=0.5)
ax.plot(dates, lows, c='blue', alpha=0.5)
ax.fill_between(dates, highs, lows, facecolor='green', alpha=0.1)

ax.set_title("2018年死亡谷每日最高&最低温度", fontsize=24)
ax.set_xlabel('', fontsize=16)
# 自适应日期格式，防止重叠
fig.autofmt_xdate()
ax.set_ylabel('温度（F）', fontsize=16)
ax.tick_params(axis='both', which='major', labelsize=16)

plt.rcParams['font.sans-serif'] = ['SimHei']
plt.show()